package com.dou.you.che.service.impl.sys;

import com.dou.you.che.comm.utils.constant.Constant;
import com.dou.you.che.comm.utils.exception.WebException;
import com.dou.you.che.core.entity.sys.SysRole;
import com.dou.you.che.core.mapper.sys.SysRoleDao;
import com.dou.you.che.service.sys.SysRoleDeptServiceI;
import com.dou.you.che.service.sys.SysRoleMenuServiceI;
import com.dou.you.che.service.sys.SysRoleServiceI;
import com.dou.you.che.service.sys.SysUserServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 角色
 * @Author 黄国刚【1058118328@qq.com】
 */
@Service
@Transactional(readOnly = true)
public class SysRoleService implements SysRoleServiceI{

    @Autowired
    private SysRoleDao sysRoleDao;
    @Autowired
    private SysRoleMenuServiceI sysRoleMenuService;
    @Autowired
    private SysUserServiceI sysUserService;
    @Autowired
    private SysRoleDeptServiceI sysRoleDeptService;

    @Override
    public SysRole queryObject(String roleId) {
        return sysRoleDao.queryObject(roleId);
    }

    @Override
    public List<SysRole> queryList(Map<String, Object> map) {
        return sysRoleDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return sysRoleDao.queryTotal(map);
    }

    @Override
    @Transactional(readOnly = false)
    public void save(SysRole role) {
        role.setCreateTime(new Date());
        sysRoleDao.save(role);

        //检查权限是否越权
        checkPrems(role);

        //保存角色与菜单关系
        sysRoleMenuService.saveOrUpdate(role.getRoleId(), role.getMenuIdList());

        //保存角色与部门关系
        sysRoleDeptService.saveOrUpdate(role.getRoleId(), role.getDeptIdList());
    }

    @Override
    @Transactional(readOnly = false)
    public void update(SysRole role) {
        role.setUpdateTime(new Date());
        sysRoleDao.update(role);

        //检查权限是否越权
        checkPrems(role);

        //更新角色与菜单关系
        sysRoleMenuService.saveOrUpdate(role.getRoleId(), role.getMenuIdList());
        //保存角色与部门关系
        sysRoleDeptService.saveOrUpdate(role.getRoleId(), role.getDeptIdList());
    }

    @Override
    @Transactional(readOnly = false)
    public void deleteBatch(String[] roleIds) {
        sysRoleDao.deleteBatch(roleIds);
    }

    @Override
    public List<String> queryRoleIdList(String createUser) {
        return sysRoleDao.queryRoleIdList(createUser);
    }

    /**
     * 检查权限是否越权
     */
    private void checkPrems(SysRole role) {
        //如果不是超级管理员，则需要判断角色的权限是否超过自己的权限
        if (Constant.SUPER_ADMIN.equals(role.getCreateUser())) {
            return;
        }

        //查询用户所拥有的菜单列表
        List<String> menuIdList = sysUserService.queryAllMenuId(role.getCreateUser());

        //判断是否越权
        if (!menuIdList.containsAll(role.getMenuIdList())) {
            throw new WebException("新增角色的权限，已超出你的权限范围");
        }
    }
}
